using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
namespace Netron.NetronLight.Demo
{
    /// <summary>
    /// Main class or form of the demo.
    /// </summary>
    public partial class MainForm : Form
    {
        #region Constructor and initialization
        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm"/> class.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            InitializeState();



        }
        /// <summary>
        /// Initializes the state.
        /// </summary>
        private void InitializeState()
        {
            this.undoButton.Enabled = false;
            this.redoButton.Enabled = false;
        }
        #endregion

        #region Undo/redo tools

        /// <summary>
        /// Handles the Click event of the undoButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void undoButton_Click(object sender, EventArgs e)
        {
            this.diagramControl1.Undo();
        }

        /// <summary>
        /// Handles the Click event of the redoButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void redoButton_Click(object sender, EventArgs e)
        {
            this.diagramControl1.Redo();
        }

        /// <summary>
        /// Updates the undo/redo buttons to reflect the history changes
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:Netron.NetronLight.HistoryChangeEventArgs"/> instance containing the event data.</param>
        private void diagramControl1_OnHistoryChange(object sender, HistoryChangeEventArgs e)
        {
            if(e.UndoText.Length == 0)
            {
                this.undoButton.Enabled = false;
                this.undoToolStripMenuItem.Enabled = false;
            }
            else
            {
                this.undoButton.Enabled = true;
                this.undoToolStripMenuItem.Enabled = true;
                this.undoButton.ToolTipText = "Undo " + e.UndoText;
                this.undoToolStripMenuItem.Text = "Undo " + e.UndoText;
            }

            if(e.RedoText.Length == 0)
            {
                this.redoButton.Enabled = false;
                this.redoToolStripMenuItem.Enabled = false;
            }
            else
            {
                this.redoButton.Enabled = true;
                this.redoToolStripMenuItem.Enabled = true;
                this.redoButton.ToolTipText = "Redo " + e.RedoText;
                this.redoToolStripMenuItem.Text = "Redo " + e.RedoText;
            }
        }
        #endregion

        #region Toolbars actions

        /// <summary>
        /// View or hide the actions strip.
        /// </summary>
        private void ViewHideActionsStrip()
        {
            actionsToolStripMenuItem.Checked = !actionsToolStripMenuItem.Checked;
            actionsStrip.Visible = actionsToolStripMenuItem.Checked;
        }

        /// <summary>
        /// View or hide the drawing strip.
        /// </summary>
        private void ViewHideDrawingStrip()
        {
            drawingToolStripMenuItem.Checked = !drawingToolStripMenuItem.Checked;
            drawingStrip.Visible = drawingToolStripMenuItem.Checked;
        }
        /// <summary>
        /// View or hide the browser strip.
        /// </summary>
        private void ViewHideBrowserStrip()
        {
            browserToolStripMenuItem.Checked = !browserToolStripMenuItem.Checked;
            browserStrip.Visible = browserToolStripMenuItem.Checked;
        }

        #endregion

        #region Browsing tools

        /// <summary>
        /// Handles the Click event of the homeNavigateButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void homeNavigateButton_Click(object sender, EventArgs e)
        {
            this.rightTabControl.SelectedTab = tabWeb;
            this.webBrowser.GoHome();
        }

        /// <summary>
        /// Handles the Click event of the backNavigateButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void backNavigateButton_Click(object sender, EventArgs e)
        {
            this.rightTabControl.SelectedTab = tabWeb;
            this.webBrowser.GoBack();
        }

        /// <summary>
        /// Handles the Click event of the forwardNavigateButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void forwardNavigateButton_Click(object sender, EventArgs e)
        {
            this.rightTabControl.SelectedTab = tabWeb;
            this.webBrowser.GoForward();
        }

        /// <summary>
        /// Handles the Click event of the stopNavigateButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void stopNavigateButton_Click(object sender, EventArgs e)
        {
            this.webBrowser.Stop();
        }

        /// <summary>
        /// Handles the Click event of the netronNavigateButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void netronNavigateButton_Click(object sender, EventArgs e)
        {
            this.rightTabControl.SelectedTab = tabWeb;
            this.webBrowser.Navigate("http://www.netronproject.com");
        }

        /// <summary>
        /// Handles the Click event of the feedbackButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void feedbackButton_Click(object sender, EventArgs e)
        {
            GotoForum();
        }

        private void ShowContent(string document)
        {
            this.rightTabControl.SelectedTab = tabWeb;
            this.webBrowser.DocumentText = document;
            
        }
        #endregion

        #region Grouping tools
        /// <summary>
        /// Handles the Click event of the groupButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void groupButton_Click(object sender, EventArgs e)
        {
            this.diagramControl1.ActivateTool("Group Tool");
        }

        /// <summary>
        /// Handles the Click event of the ungroupButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void ungroupButton_Click(object sender, EventArgs e)
        {
            this.diagramControl1.ActivateTool("Ungroup Tool");
        }
        #endregion

        #region Z-ordering tools
        /// <summary>
        /// Handles the Click event of the sendToBackButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void sendToBackButton_Click(object sender, EventArgs e)
        {
            this.diagramControl1.ActivateTool("SendToBack Tool");
        }

        /// <summary>
        /// Handles the Click event of the sendBackwardsButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void sendBackwardsButton_Click(object sender, EventArgs e)
        {
            this.diagramControl1.ActivateTool("SendBackwards Tool");
        }

        /// <summary>
        /// Handles the Click event of the sendForwardButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void sendForwardButton_Click(object sender, EventArgs e)
        {
            this.diagramControl1.ActivateTool("SendForwards Tool");
        }

        /// <summary>
        /// Handles the Click event of the sendToFrontButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void sendToFrontButton_Click(object sender, EventArgs e)
        {
            this.diagramControl1.ActivateTool("SendToFront Tool");
        }

        #endregion

        #region Standard tools
        /// <summary>
        /// Handles the Click event of the newDiagramButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void newDiagramButton_Click(object sender, EventArgs e)
        {
            NewDiagram();
        }

        private void NewDiagram()
        {
            DialogResult res = MessageBox.Show("Are you sure?", "New diagram", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (res == DialogResult.Yes)
                this.diagramControl1.NewDiagram();
        }

        /// <summary>
        /// Handles the Click event of the saveButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void saveButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not implemented yet.", "Hang on", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// Handles the Click event of the openDiagramButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void openDiagramButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not implemented yet.", "Hang on", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// Handles the Click event of the printButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void printButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not implemented yet.", "Hang on", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// Handles the Click event of the cutButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void cutButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not implemented yet.", "Hang on", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// Handles the Click event of the copyButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void copyButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not implemented yet.", "Hang on", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// Handles the Click event of the pasteButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void pasteButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not implemented yet.", "Hang on", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// Handles the Click event of the exitMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Handles the Click event of the helpButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void helpButton_Click(object sender, EventArgs e)
        {
            GotoForum();

        }

        private void GotoForum()
        {
            this.rightTabControl.SelectedTab = tabWeb;
            webBrowser.Navigate("http://sourceforge.net/forum/forum.php?forum_id=237527");
        }
        #endregion

        #region Context menu of the toolbars
        /// <summary>
        /// Handles the Click event of the standardToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void standardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            standardToolStripMenuItem.Checked = !standardToolStripMenuItem.Checked;
            mainStrip.Visible = standardToolStripMenuItem.Checked;
        }
        /// <summary>
        /// Handles the Click event of the drawingToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void drawingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ViewHideDrawingStrip();
        }
        /// <summary>
        /// Handles the Click event of the browserToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void browserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ViewHideBrowserStrip();
        }
        /// <summary>
        /// Handles the Click event of the actionsToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void actionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ViewHideActionsStrip();
        }
        #endregion

        #region Diagram control handler links
        private void diagramControl1_OnShowSelectionProperties(object sender, SelectionEventArgs e)
        {

            this.propertyGrid.SelectedObjects = e.SelectedObjects;
        }
        #endregion

        #region Tab selection actions
        private void diagramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rightTabControl.SelectedTab = tabDiagram;
        }

        private void shapesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            leftTabControl.SelectedTab = tabShapes;
        }

        private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            leftTabControl.SelectedTab = tabProperties;
        }

        private void browserToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            rightTabControl.SelectedTab = tabWeb;
        }
        #endregion

        #region Drawing tools
        /// <summary>
        /// Handles the Click event of the drawRectangleButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void drawRectangleButton_Click(object sender, EventArgs e)
        {
            this.diagramControl1.ActivateTool("Rectangle Tool");
        }
        /// <summary>
        /// Handles the Click event of the drawEllipseButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void drawEllipseButton_Click(object sender, EventArgs e)
        {
            this.diagramControl1.ActivateTool("Ellipse Tool");
        }

        /// <summary>
        /// Handles the Click event of the drawingButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void drawingButton_Click(object sender, EventArgs e)
        {
            ViewHideDrawingStrip();
        }

        /// <summary>
        /// Handles the MouseDown event of the shapesListView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void shapesListView_MouseDown(object sender, MouseEventArgs e)
        {
            ListViewItem item = this.shapesListView.GetItemAt(e.X, e.Y);
            if(item != null)
            {
                this.diagramControl1.DoDragDrop(item.Tag.ToString(), DragDropEffects.All);
            }

        }

        /// <summary>
        /// Handles the Click event of the connectionButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void connectionButton_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("Not implemented yet.", "Hang on", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.diagramControl1.ActivateTool("Connection Tool");
        }

        /// <summary>
        /// Handles the Click event of the connectorMoverButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void connectorMoverButton_Click(object sender, EventArgs e)
        {
            this.diagramControl1.ActivateTool("Connector Mover Tool");
        }

        private void textToolButton_Click(object sender, EventArgs e)
        {
            this.diagramControl1.ActivateTool("Text Tool");
        }
        #endregion

        /// <summary>
        /// Handles the Click event of the aboutToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowAbout();            
        }

        /// <summary>
        /// Handles the Click event of the netronEULAToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void netronEULAToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowEula();          
        }
        /// <summary>
        /// Shows the about-info.
        /// </summary>
        private void ShowAbout()
        {
            try
            {
                Stream stream= Assembly.GetExecutingAssembly().GetManifestResourceStream("Netron.NetronLight.Demo.Resources.AboutTemplate.htm");
                if (stream != null)
                {
                    StreamReader reader = new StreamReader(stream);
                    string template = reader.ReadToEnd();
                    template = template.Replace("$title$", AssemblyInfo.AssemblyTitle);
                    template = template.Replace("$version$", AssemblyInfo.AssemblyVersion);
                    template = template.Replace("$company$", AssemblyInfo.AssemblyCompany);
                    template = template.Replace("$description$", AssemblyInfo.AssemblyDescription);

                    reader.Close();
                    stream.Close();
                    ShowContent( template);

                }
                else
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("Title: {0}", AssemblyInfo.AssemblyTitle);
                    sb.Append("<br/>");
                    sb.AppendFormat("Version: {0}", AssemblyInfo.AssemblyVersion);
                    sb.Append("<br/>");
                    sb.AppendFormat("Company: {0}", AssemblyInfo.AssemblyCompany);
                    sb.Append("<br/>");
                    sb.AppendFormat("Description: {0}", AssemblyInfo.AssemblyDescription);

                    this.ShowContent(sb.ToString());
                }
            }
            catch
            {
                this.ShowContent("Unable to fetch the assembly information.");
            }

        }
        /// <summary>
        /// Shows the EULA.
        /// </summary>
        private void ShowEula()
        {
            string lic = string.Empty;

            try
            {
                System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Netron.NetronLight.Demo.Resources.Netron EULA.htm");
                if (stream != null)
                {
                   StreamReader reader = new System.IO.StreamReader(stream);
                    lic = reader.ReadToEnd();
                    ShowContent(lic);
                    reader.Close();
                    stream.Close();
                }
            }
            catch(Exception)
            {
            }
        }

        private void classDocumentationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\html\\index.html";
            //this.webBrowser.Url = new Uri(path);



        }

        private void diagramControl1_OnEntityAdded(object sender, EntityEventArgs e)
        {
            string msg = string.Empty;
            if (e.Entity is IShape)
                msg = "New shape '" + e.Entity.EntityName + "' added.";
            else if (e.Entity is IConnection)
                msg = "New connection added.";

            if(msg.Length>0)
                ShowStatusText(msg);
            if (e.Entity is ComplexRectangle)
            {
                ComplexRectangle shape = e.Entity as ComplexRectangle;

                try
                {
                    shape.Services[typeof(IMouseListener)]= new MyPlugin(shape);
                }
                catch (ArgumentException exc)
                {
                    ShowStatusText(exc.Message);
                    
                }
               
                
            }
            else if(e.Entity is ImageShape)
            {
                Bitmap bmp = GetSampleImage();
                if(bmp != null)
                    (e.Entity as ImageShape).Image = bmp;
            }
        }

        private Bitmap GetSampleImage()
        {

           Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Netron.NetronLight.Demo.Resources.Talking.gif");
           Bitmap bmp = null;
           if(stream != null)
           {
               bmp = Bitmap.FromStream(stream) as Bitmap;
               stream.Close();
           }
           return bmp;
        }

        private void ShowStatusText(string text)
        {
            this.statusLabel1.Text = text;
            this.statusLabel1.Visible = true;
            statusTimer.Start();
        }

        private void statusTimer_Tick(object sender, EventArgs e)
        {
            statusTimer.Stop();
            this.statusLabel1.Visible = false;
        }

        private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            statusProgressBar.Visible = true;
            statusProgressBar.Value = 0;
        }

        private void webBrowser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            statusProgressBar.Value = Convert.ToInt32(Math.Round(e.CurrentProgress * (double) 100 / e.MaximumProgress, 0));
        }

        private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            statusProgressBar.Visible = false;
        }

        private void shapesListView_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {
            Cursor.Current = Cursors.HSplit;
        }

        private void propertiesButton_Click(object sender, EventArgs e)
        {
            this.leftTabControl.SelectedTab = tabProperties;
            this.propertyGrid.SelectedObjects = diagramControl1.SelectedItems.ToArray();
        }

        private void newDiagramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewDiagram();
        }

        

        

        
        

       
    }
    public class MyPlugin : IMouseListener
    {
        ComplexRectangle shape;
        public MyPlugin(ComplexRectangle shape)
        {
            this.shape = shape;
        }



        #region IClickListener Members

        public void MouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            this.shape.ShapeColor = ArtPallet.RandomColor;
            (this.shape.Children[0] as LabelMaterial).Text = this.shape.ShapeColor.ToString();
            //foreach (IPaintable material in shape.Children)
            //{
            //    //material.;
            //}
        }

        #endregion


        public void MouseMove(System.Windows.Forms.MouseEventArgs e)
        {

        }

        public void MouseUp(System.Windows.Forms.MouseEventArgs e)
        {

        }
    }
}